/**
 * 封装jquery的ajax请求
 * 
 */
/*******************************************************************************
 AjaxTools.ajax({ 
	url:"select.do", //请求地址 
	method:"post", //请求方式post get [可选]默认post 
	async:true, //true 异步 false 同步 [可选]默认true 
	showMsg:true, //是否要进度提示 [可选]默认true 
	msg : "正在加载中....", //提示信息 [可选] 
	form:"form1", //需提交的表单id 
	params:{ //form表单外需提交的参数 
		"name":"11", //参数名 : 值
		"SQLID":"selectModule" 
	}, 
	callback:[callFun1,callFun2] //回调函数 
 });
******************************************************************************/
var AjaxTools = (function() {

	var getParmValue = function(form, params) {
		var str = "";
		// 从指定form表单取值
		if (form) {
			str = getContent(document.getElementById(form));
		}
		// 组装额外指定的参数
		if (params) {
			for ( var key in params) {
				if (str.length > 0)
					str += "&";
				str += key + "=" + params[key];
			}
		}
		return str;
	};
	// 获取参数ajax对象参数
	var getArgs = function(json) {
		var async = true;
		if (json.async != null)
			async = json.async;
		var showMsg = true;
		if (json.showMsg != null)
			showMsg = json.showMsg;
		var o = {
			url : json.url,
			method : json.method ? json.method : "POST",
			async : async,
			showMsg : showMsg,
			msg : json.msg ? json.msg : "数据处理中...",
			form : json.form ? json.form : null,
			params : json.params ? json.params : null,
			callback : json.callback ? json.callback : []
		};
		return o;
	};
	// 获取form表单数据
	var getContent = function(theform) {
		var els = theform.elements;
		var len = els.length;
		var queryString = "";
		this.addField = function(name, value) {
			if (queryString.length > 0) {
				queryString += "&";
			}
			queryString += name + "=" + value;
		};
		for ( var i = 0; i < len; i++) {
			var el = els[i];
			if (!el.disabled) {
				switch (el.type) {
				case "text":
				case "password":
				case "hidden":
				case "textarea":
					this.addField(el.name, el.value);
					break;
				case "select-one":
					if (el.selectedIndex >= 0) {
						this.addField(el.name,
								el.options[el.selectedIndex].value);
					}
					break;
				case "select-multiple":
					for ( var j = 0; j < el.options.length; j++) {
						if (el.options[j].selected) {
							this.addField(el.name, el.options[j].value);
						}
					}
					break;
				case "checkbox":
				case "radio":
					if (el.checked) {
						this.addField(el.name, el.value);
					}
					break;
				}
			}
		}
		return queryString.substring(0, queryString.length);
	};
	// 显示蒙层
	var showLoadingMessage = function(msg) {
		// 蒙层
		var div = document.getElementById("JXW__loading_div");

		if (div == null) {
			div = document.createElement("DIV");
			document.body.appendChild(div);
			div.id = "JXW__loading_div";
			div.style.position = "absolute";
			div.style.top = "0px";
			div.style.filter = "alpha(opacity=20)";
			div.style.background = "#777";
			div.style.zindex = "2";
			div.style.left = "0px";
		}
		var width = document.body.clientWidth > document.body.scrollWidth?document.body.clientWidth:document.body.scrollWidth;
		var height = document.body.clientHeight > document.body.scrollHeight?document.body.clientHeight:document.body.scrollHeight;
		div.style.width = width;
		div.style.height = height;
		div.style.display = "";
		// 进度提示
		var divImg = document.getElementById("JXW__loading_div_IMG");
		if (divImg == null) {
			divImg = document.createElement("DIV");
			document.body.appendChild(divImg);
			divImg.id = "JXW__loading_div_IMG";
            divImg.className = "ajaxLoading";
			divImg.style.position = "absolute";
			divImg.innerHTML = "<table border='0' cellpadding='0' cellspacing='0' style='width: 100%;'><tr><td style='height: 66px;'>&nbsp;</td></tr><tr><td style='color:red;text-align: center;'>"+msg+"</td></tr></table>";
		}

		divImg.style.top = document.body.clientHeight / 3;
		divImg.style.left = document.body.clientWidth / 3;
		divImg.style.display = "";
	};
	// 隐藏蒙层
	var hideLoadingMessage = function() {
		var div = document.getElementById("JXW__loading_div");
		if (div != null)
			div.style.display = "none";
		var divImg = document.getElementById("JXW__loading_div_IMG");
		if (divImg != null)
			divImg.style.display = "none";
	};

	return {
		/**
		 * 发送ajax请求
		 * 
		 * @param xhrArgs
		 */
		ajax : function(xhrArgs) {
			var args = getArgs(xhrArgs);
			if (args.showMsg)
				showLoadingMessage(args.msg);// 显示蒙层

			var params = getParmValue(args.form, args.params);
			var url = "";
			if (args.method != null && args.method == "get"
					|| args.method == "GET") {
				url = args.url + "?" + params;
			} else {
				url = args.url;
			}

			$.ajax({
				url : url,
				async : args.async,
				type : args.method,
				dataType : "text",
				context : args,// ajax请求的上下文
				data : params,
				error : function(XMLHttpRequest, textStatus, errorThrown) {// 当请求失败时调用的函数
					// 调用本次AJAX请求时传递的options参数
					alert('操作提示\n操作失败原因:' + textStatus + "\n" + errorThrown);
					if (args.showMsg)
						hideLoadingMessage();
				},
				success : function(data, textStatus) {// 当请求成功时调用的函数
					// this指向Ajax相关回调函数的上下文context
					if (this.callback) {
						var rtnData = eval("(" + data + ")");
						for ( var i = 0; i < this.callback.length; i++)
							this.callback[i](rtnData);
					}
					if (this.showMsg)
						hideLoadingMessage();
				},
				// 当请求完成时调用的函数
				complete : function() {
				}
			});
		}
	};
})();